#include "stdafx.h"

#include "cmdcompiler.h"
#include <lean/logging/log.h>
#include <lean/logging/log_stream.h>

namespace funpl
{

namespace shell
{

/// Command line actions.
namespace action
{
	/// Enumeration.
	enum t
	{
		help,
		compile
	};
}

/// Converts the given string into a compiler action or help on error.
action::t to_action(const char *name)
{
	action::t action = action::help;

	if (_stricmp(name, "compile") == 0)
		action = action::compile;
//	else if (_stricmp(name, "help") == 0)
//		action = action::help;

	return action;
}

/// Prints the global command line help.
void print_help(std::ostream &stream)
{
	stream << " Syntax: funpl {<action> | help <action>}"  << std::endl << std::endl;

	stream << " Actions:"  << std::endl;
	stream << "  compile        Passes the given arguments to the compiler" << std::endl;
	stream << "  help           Displays this help"  << std::endl;
}

/// Prints the global command line help.
int print_help(int argc, const char* argv[])
{
	action::t action = (argc >= 1) ? to_action(argv[0]) : action::help;

	switch (action)
	{
	case action::help:
		print_help(std::cout);
		return 0;

	case action::compile:
		print_compiler_help(std::cout);
		return 0;

	default:
		LEAN_ASSERT(false);
	}

	return 0;
}

} // namespace

} // namespace

int main(int argc, const char* argv[])
{
	lean::log_stream log_cout(&std::cout);
	lean::error_log().add_target(&log_cout);
	lean::info_log().add_target(&log_cout);

	using namespace funpl::shell;

	action::t action = (argc >= 2) ? to_action(argv[1]) : action::help;

	switch (action)
	{
	case action::help:
		return print_help(argc - 2, &argv[2]);

	case action::compile:
		return run_compiler(argc - 2, &argv[2]);

	default:
		LEAN_ASSERT(false);
	}

	return 0;
}
